import * as xlsx from 'xlsx'
export const jsonToExcel = (options: {
    data: any[],
    header: Record<string, string>,
    fileName: string,
    bookType:xlsx.BookType
}) => {
    const wb = xlsx.utils.book_new()

    // 创建工作表 worksheet
    if(options.header) {
        options.data = options.data.map(item => {
            const obj: Record<string, any> = {}
            for(const key in item) {
                if(options.header[key]) {
                    obj[options.header[key]] = options.header[key]
                }else {
                    obj[key] = item[key]
                }
            }
            return obj
        })
    }

    const ws = xlsx.utils.json_to_sheet(options.data)

    // 把工作表放到工作簿中
    xlsx.utils.book_append_sheet(wb, ws)

    // 生产的数据保存
    xlsx.writeFile(wb, '测试表格.xlsx', {
        bookType: options.bookType || 'xlsx'
    })

}